﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;
using DTO;
using DevComponents.DotNetBar;

namespace QLST
{
    public partial class frmSanPham : Form
    {
        SanPhamBUS cls = new SanPhamBUS();
        SanPhamDTO p = new SanPhamDTO();
        bool flag;
        public frmSanPham()
        {
            InitializeComponent();
        }

        private void frmSanPham_Load(object sender, EventArgs e)
        {
            ClearTextBox();
            LockTextBox(false);
            LockButton(true);
            LoadSP();
        }
        public void LoadSP()
        {
            gvSanPham.DataSource = cls.LoadSanPham();
        }

        private void btnNVThem_Click(object sender, EventArgs e)
        {
            LockButton(false);
            LockTextBox(true);
            ClearTextBox();
            gvSanPham.Enabled = false;
            txtTenSP.Focus();
            cmbKho.DataSource = cls.Kho();
            cmbKho.DisplayMember = "MaKho";
            cmbKho.ValueMember = "MaKho";
            cmbQuay.DataSource = cls.Quay();
            cmbQuay.DisplayMember = "MaQuay";
            cmbQuay.ValueMember = "MaQuay";
        }

        private void btnNVXoa_Click(object sender, EventArgs e)
        {
            p.MaSP  = txtMaSP.Text;
            cls.Del_SP (p);
            frmSanPham_Load(sender, e);
        }

        private void btnNVSua_Click(object sender, EventArgs e)
        {
            LockTextBox(true);
            LockButton(false);
            gvSanPham.Enabled = false;
            txtTenSP.Focus();   
        }
        private void btnNVLuu_Click(object sender, EventArgs e)
        {
            if (txtMaSP.Text == "")
            {
                flag = true;
            }
            else if (txtMaSP.Text != "")
            {
                flag = false;
            }
            p.MaSP = txtMaSP.Text;
            p.MaKho = cmbKho.Text;
            p.MaQuay = cmbQuay.Text;
            p.TenSP = txtTenSP.Text;
            p.NhaCC = txtNhaCC.Text;
            p.DonGia = float.Parse(txtDonGia.Text);
            p.GiaBan = float.Parse(txtGiaBan.Text);
            p.GiaKhuyenMai = float.Parse(txtGiaKM.Text);
            cls.IsUp_SP(p, flag);
            frmSanPham_Load(sender, e);
        }
        private void gvSanPham_SelectionChanged(object sender, EventArgs e)
        {
            txtMaSP.Text = gvSanPham.CurrentRow.Cells["colMaSP"].Value.ToString();
            cmbKho.Text = gvSanPham.CurrentRow.Cells["colMaKho"].Value.ToString();
            cmbQuay.Text = gvSanPham.CurrentRow.Cells["colMaQuay"].Value.ToString();
            txtNhaCC.Text = gvSanPham.CurrentRow.Cells["colNhaCC"].Value.ToString();
            txtTenSP.Text = gvSanPham.CurrentRow.Cells["colTenSP"].Value.ToString();
            txtDonGia.Text = gvSanPham.CurrentRow.Cells["colDonGia"].Value.ToString();
            txtGiaBan.Text = gvSanPham.CurrentRow.Cells["colGiaBan"].Value.ToString();
            txtGiaKM.Text = gvSanPham.CurrentRow.Cells["ColGiaKM"].Value.ToString();
        }

        private void btnNVHuy_Click(object sender, EventArgs e)
        {
            ClearGV();
            txtTimKiem.Text = "";
            LockButton(true);
            LockTextBox(false);
            gvSanPham.Enabled = true;
            frmSanPham_Load (sender ,e);
        }

        private void btnNVTim_Click(object sender, EventArgs e)
        {
            p.MaSP = txtTimKiem.Text;
            p.TenSP = txtTimKiem.Text;
            if (cbMaSP.Checked == true)
            {
                flag = true;
            }
            else
            {
                flag = false;
            }
            gvSanPham.ClearSelection();
            try
            {
                gvSanPham .DataSource = cls .Tim_SP (p, flag);
            }
            catch
            {
                MessageBoxEx.Show("Bạn nhập sai Mã sản phẩm đơn hoặc tên sản phẩm!", "PASSWORD CHANGED", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void buttonX1_Click(object sender, EventArgs e)
        {
            txtTimKiem.Text = "";
            frmSanPham_Load(sender, e);
        }

        private void LockButton(Boolean f)
        {
            btnNVThem.Enabled = f;
            btnNVSua.Enabled = f;
            btnNVXoa.Enabled = f;
            btnNVLuu.Enabled = !f;
            btnNVHuy.Enabled = !f;
        }
        private void ClearGV()
        {
            gvSanPham.ClearSelection();
        }
        public void ClearTextBox()
        {
            txtMaSP.Text = "";
            txtTenSP.Text = "";
            txtNhaCC.Text = "";
            txtDonGia.Text = "";
            txtGiaBan.Text = "";
            txtGiaKM.Text = "";
        }
        private void LockTextBox(Boolean f)
        {
            txtMaSP .Enabled = f;
            txtTenSP.Enabled = f;
            txtNhaCC.Enabled = f;
            txtDonGia.Enabled = f;
            txtGiaBan.Enabled = f;
            txtGiaKM.Enabled = f;
            cmbKho.Enabled = f;
            cmbQuay.Enabled = f;

            if (f == false)
            {
                txtMaSP.BackColor = Color.LemonChiffon;
                txtTenSP.BackColor = Color.LemonChiffon;
                txtNhaCC.BackColor = Color.LemonChiffon;
                txtDonGia.BackColor = Color.LemonChiffon;
                txtGiaBan.BackColor = Color.LemonChiffon;
                txtGiaKM.BackColor = Color.LemonChiffon;
                cmbKho.BackColor = Color.LemonChiffon;
                cmbQuay.BackColor = Color.LemonChiffon;
            }
            else if (f == true)
            {
                txtMaSP.BackColor = Color.White;
                txtTenSP.BackColor = Color.White;
                txtNhaCC.BackColor = Color.White;
                txtDonGia.BackColor = Color.White;
                txtGiaBan.BackColor = Color.White;
                txtGiaKM.BackColor = Color.White;
                cmbKho.BackColor = Color.White;
                cmbQuay.BackColor = Color.White;
            }
        }

        
        
    }
}
